package com.hongliang.chat.protocol;

import com.hongliang.chat.message.LoginRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;

/**
 * @author Hongliang Zhu
 * @create 2022-11-11 22:43
 */
public class MessageCodecTest {

    public static void main(String[] args) throws Exception {
        EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler(),
                new LengthFieldBasedFrameDecoder(
                        1024, 12, 4, 0, 0),
                new MessageCodecShareable());
// encode
        LoginRequestMessage message = new LoginRequestMessage("zhangsan", "123");
//        channel.writeOutbound(message);

        // decode00000
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null, message, buffer);

        ByteBuf s1 = buffer.slice(0, 100);
        s1.retain();
        ByteBuf s2 = buffer.slice(100, buffer.readableBytes() - 100);
        channel.writeInbound(s1, s2);

    }
}
